<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="老人姓名" prop="elderName">
              <el-input v-model="queryParams.elderName" placeholder="请输入老人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleStepAssessment" v-hasPermi="['nursing:abilityAssessment:add']">开始长者评估</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['nursing:abilityAssessment:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['nursing:abilityAssessment:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['nursing:abilityAssessment:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="abilityAssessmentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="老人姓名" align="center" prop="elderName" min-width="120" />
        <el-table-column label="评估人" align="center" prop="assessor" min-width="120" />
        <el-table-column label="评估日期" align="center" prop="assessDate" min-width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.assessDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总分" align="center" prop="totalScore" min-width="80" />
        <el-table-column label="护理等级" align="center" prop="careLevel" min-width="100" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleView(scope.row)">查看详情</el-button>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['nursing:abilityAssessment:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['nursing:abilityAssessment:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改老年人能力评估对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="abilityAssessmentFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="老人姓名" prop="elderName">
          <el-input v-model="form.elderName" placeholder="请输入老人姓名" />
        </el-form-item>
        <el-form-item label="评估日期" prop="assessDate">
          <el-date-picker clearable
            v-model="form.assessDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择评估日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="自理能力" prop="selfCare">
          <el-input v-model="form.selfCare" placeholder="请输入自理能力" />
        </el-form-item>
        <el-form-item label="行动能力" prop="mobility">
          <el-input v-model="form.mobility" placeholder="请输入行动能力" />
        </el-form-item>
        <el-form-item label="进食能力" prop="eating">
          <el-input v-model="form.eating" placeholder="请输入进食能力" />
        </el-form-item>
        <el-form-item label="穿衣能力" prop="dressing">
          <el-input v-model="form.dressing" placeholder="请输入穿衣能力" />
        </el-form-item>
        <el-form-item label="如厕能力" prop="toileting">
          <el-input v-model="form.toileting" placeholder="请输入如厕能力" />
        </el-form-item>
        <el-form-item label="洗澡能力" prop="bathing">
          <el-input v-model="form.bathing" placeholder="请输入洗澡能力" />
        </el-form-item>
        <el-form-item label="认知能力" prop="cognitive">
          <el-input v-model="form.cognitive" placeholder="请输入认知能力" />
        </el-form-item>
        <el-form-item label="沟通能力" prop="communication">
          <el-input v-model="form.communication" placeholder="请输入沟通能力" />
        </el-form-item>
        <el-form-item label="社交能力" prop="social">
          <el-input v-model="form.social" placeholder="请输入社交能力" />
        </el-form-item>
        <el-form-item label="情绪状态" prop="emotion">
          <el-input v-model="form.emotion" placeholder="请输入情绪状态" />
        </el-form-item>
        <el-form-item label="总分" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总分" />
        </el-form-item>
        <el-form-item label="护理等级" prop="careLevel">
          <el-input v-model="form.careLevel" placeholder="请输入护理等级" />
        </el-form-item>
        <el-form-item label="评估人" prop="assessor">
          <el-input v-model="form.assessor" placeholder="请输入评估人" />
        </el-form-item>
        <el-form-item label="护理建议" prop="suggestions">
            <el-input v-model="form.suggestions" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AbilityAssessment" lang="ts">
import { listAbilityAssessment, getAbilityAssessment, delAbilityAssessment, addAbilityAssessment, updateAbilityAssessment } from '@/api/nursing/abilityAssessment';
import { AbilityAssessmentVO, AbilityAssessmentQuery, AbilityAssessmentForm } from '@/api/nursing/abilityAssessment/types';
import { getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();

const abilityAssessmentList = ref<AbilityAssessmentVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const abilityAssessmentFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: AbilityAssessmentForm = {
  id: undefined,
  elderName: undefined,
  assessDate: undefined,
  selfCare: undefined,
  mobility: undefined,
  eating: undefined,
  dressing: undefined,
  toileting: undefined,
  bathing: undefined,
  cognitive: undefined,
  communication: undefined,
  memory: undefined,
  social: undefined,
  emotion: undefined,
  sleep: undefined,
  totalScore: undefined,
  careLevel: undefined,
  assessor: undefined,
  suggestions: undefined,
  remarks: undefined,
}
const data = reactive<PageData<AbilityAssessmentForm, AbilityAssessmentQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    elderName: undefined,
    params: {
    }
  },
  rules: {
    id: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
    assessDate: [
      { required: true, message: "评估日期不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询老年人能力评估列表 */
const getList = async () => {
  loading.value = true;
  const res = await listAbilityAssessment(queryParams.value);
  abilityAssessmentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  abilityAssessmentFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: AbilityAssessmentVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加老年人能力评估";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: AbilityAssessmentVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getAbilityAssessment(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改老年人能力评估";
}

/** 提交按钮 */
const submitForm = () => {
  abilityAssessmentFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateAbilityAssessment(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addAbilityAssessment(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: AbilityAssessmentVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除老年人能力评估编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delAbilityAssessment(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('nursing/abilityAssessment/export', {
    ...queryParams.value
  }, `abilityAssessment_${new Date().getTime()}.xlsx`)
}

/** 逐项评估按钮操作 */
const handleStepAssessment = () => {
  router.push('/nursing/abilityAssessment/stepAssessment');
}

/** 查看详情按钮操作 */
const handleView = (row: AbilityAssessmentVO) => {
  router.push({
    path: '/nursing/abilityAssessment/stepAssessment',
    query: {
      id: row.id,
      mode: 'view'
    }
  });
}

onMounted(() => {
  getList();
});
</script>
